package com.rljk.controller.app;

import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.redis.RedisService;
import com.rljk.service.CaidanManager;
import com.rljk.service.impl.ExtensionService;
import com.rljk.service.impl.UserDownConService;
import com.rljk.utils.AppUtil;
import com.rljk.utils.Const;
import com.rljk.utils.Tools;
import com.rljk.utils.ToolsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:   YASUO
 * Date:     2018/8/22 17:44
 * Description:app公众号推荐接口
 */
@RestController
@Slf4j
@RequestMapping("/extension")
public class ExtensionInController extends BaseController{

    @Resource(name = "extensionService")
    private ExtensionService extensionService;

    @Resource(name = "caidanService")
    private CaidanManager caidanService;

    @Resource(name = "userdownconService")
    private UserDownConService userDownConService;

    @Autowired
    RedisService redisService;

    /**
     * 获取热推app下载或者公众号推荐
     * author:YASUO
     * date:2018/8/22
     */
    @RequestMapping(value="/getapporpublicnum")
    public Object getAppOrPublicNum() {
        log.info("获取热推app下载或者公众号推荐");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        List<Object> list = new ArrayList<>();
        try {
            if (Tools.checkKey("APPPULBICNUMBER", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("getAppOrPublicNum",pd)){
                    String USER_ID = redisService.get("TOKEN");
                    pd.put("USER_ID",USER_ID);
                    pd.put("CAIDAN_TYPE","3");//1-代表首页菜单，2代表任务页面菜单3-扫一扫下载分类 
                    List<PageData> caidan = caidanService.listAll(pd);
                    List<PageData> scan_list = new ArrayList<>();
                    for (PageData p : caidan) {
                        PageData scan = new PageData();
                        scan.put("candan",p.get("NAME"));
                        p.put("QRCODE_TYPE",pd.get("QRCODE_TYPE"));
                        scan_list = extensionService.getAppOrPublicNum(p);
                        if(Tools.notEmptyList(scan_list)){
                            for (PageData p1 : scan_list) {
                                pd.put("EXTENSION_ID",p1.get("EXTENSION_ID"));
                                PageData byId = userDownConService.findById(pd);
                                if(byId!=null){
                                    p1.put("DOWN_FOLLOW",1);//已下载或者已关注
                                }else {
                                    p1.put("DOWN_FOLLOW",0); //未下载或者未关注
                                }

                            }
                            scan.put("scan_list",scan_list);
                            list.add(scan);
                        }
                    }
                    if(Tools.isEmptyList(list)){
                        return Result.error(CodeMsg.EMPTY);
                    }
                    return Result.success(list);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }

    }


    /**
     * 获取热推app下载或者公众号推荐详情
     * author:YASUO
     * date:2018/8/22
     */
    @RequestMapping(value="/getapporpublicnumdet")
    public Object getAppOrPublicNumDetail() {
        log.info("获取热推app下载或者公众号推荐详情");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        try {
            if (Tools.checkKey("APPPULBICNUMBERDET", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("getAppOrPublicNumDetail",pd)){
                    PageData extension = extensionService.getAppOrPublicNumDetail(pd.getString("EXTENSION_ID"));
                    if(extension==null){
                        return Result.error(CodeMsg.EMPTY);
                    }
                    return Result.success(extension);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }

    }





}
